package com.schoolCommunity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.schoolCommunity.entity.Information;
import com.schoolCommunity.entity.MailboxMember;
import com.schoolCommunity.mapper.MailboxMemberMapper;
import com.schoolCommunity.service.MailboxMemberService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author YTH
 * @since 2020-10-14
 */
@Service
public class MailboxMemberServiceImpl extends ServiceImpl<MailboxMemberMapper, MailboxMember> implements MailboxMemberService {

    @Resource
    private MailboxMemberMapper mailboxMemberMapper;

    @Override
    public Information memberGetNoMail(long memberId) {
        Information information = new Information();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("get_member_id", memberId);
        queryWrapper.eq("isGet", 0);
        List list = mailboxMemberMapper.selectList(queryWrapper);
        if (list.isEmpty()) {
            information.setMessage("没有未阅读的数据").setCode(404);
        } else {
            information.setMessage("数据获取成功").setCode(200);
        }
        return information;
    }

    @Override
    public Information memberAllMail(long memberId) {
        Information information = new Information();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("get_member_id", memberId);
        List list = mailboxMemberMapper.selectList(queryWrapper);
        if (list.isEmpty()) {
            information.setMessage("没有未阅读的数据").setCode(404);
        } else {
            information.setMessage("数据获取成功").setCode(200).setData(list);
        }
        return information;
    }

    @Override
    public Information adminSendToMember(MailboxMember mailboxMember) {
        Information information = new Information();
        mailboxMember.setSendTime(new Date());
        int insert = mailboxMemberMapper.insert(mailboxMember);
        if (insert > 0) {
            information.setMessage("发送成功").setCode(200);
        } else {
            information.setMessage("发送失败").setCode(404);
        }
        return information;
    }

    @Override
    public Information GetSendFromAdmin(long adminId) {
        Information information = new Information();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("send_admin_id", adminId);
        List list = mailboxMemberMapper.selectList(queryWrapper);
        if (list.isEmpty()) {
            information.setMessage("你没有发送过邮箱").setCode(404);
        } else {
            information.setMessage("邮箱获取成功").setCode(200).setData(list);
        }
        return information;
    }

    @Override
    public Information MemberQueRenMail(long MailId) {
        Information information = new Information();
        MailboxMember mailboxMember = new MailboxMember();
        mailboxMember.setId(MailId);
        mailboxMember.setIsGet(1);
        mailboxMember.setGetTime(new Date());
        int i = mailboxMemberMapper.updateById(mailboxMember);
        if(i>0){
            information.setCode(200).setMessage("确认阅读");
        }else{
            information.setCode(404).setMessage("确认失败");
        }
        return information;
    }

}
